<?php
namespace app\api\controller;
use app\common\controller\ApiBase;
use app\common\library\ErrorType;
//use app\common\library\YuDa;
//use app\common\library\YuResult;
//use app\common\model\Type;
use app\common\library\Sign;
use think\Exception;
use think\Db;
use think\facade\Cache;
//use app\common\model\UrlPublic;
//use app\common\model\UrlPerson;
//use app\common\library\YuUtil;


/**
 * @title 个人Url 类
 * @description public URL 管理
 * @package app\api\controller
 */
class Perdata extends ApiBase
{
    /**
     * @var 用户登陆的id
     */
    protected $uid;
    /**
     * @var array 前置操作，这里是检查登录
     */
    protected $beforeActionList = [
        'checkAuth'=>['except'=>'test,simpleshow,peraddbywxnickname']
    ];

    /**
     * @title 检查登录
     * @description 检查登录
     * @author Admin ldy
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/9/19  9:36
     */
    protected function checkAuth(){
        /// 如需登录，调用此方法鉴权
        /// 注解1：如果已经登录，则会获取到uid;否则会直接打印错误信息，不执行下面的代码
        /// 注解2：该方法走的是缓存，不通过数据库检查
        /// 注解3：如果没有token信息，需要调登录接口，重新获取token，并保存，token有效期暂定为30天
        /// 注解4：该类所有方法需要登录鉴权，故预先加载
        $this->uid = $this->getUidByCheck();
        //用户如果操作了个人的数据，就代表今天签到了，这个可以用来记录活跃量！！每天是否调用了接口？
        Sign::recordToday($this->uid);
    }

    /**
     * @title 个人URL的增加(根据url_string)
     * @description 个人URL的增加(根据url_string)。同时检查了公共URL表、插入公共URL表。插入个人URL表。操作复杂，速度较慢，但能保证数据插入的正确性。
     * @author Admin ldy
     * @url /api/perdata/perAddByU
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:u  type:string require:1 default:1 desc:核心数据
     * @param name:typeid  type:string require:1 default:1 desc:类型id
     * @param name:nickname  type:string require:1 default:1 desc:标题
     * @param name:desc  type:string require:1 default:1 desc:描述
     * @param name:key_word  type:string require:1 default:1 desc:关键词
     * @time 2018/9/25  17:30
     */
    public function perAddByU(){
        $url = $this->request->param('u');
        $typeid = $this->request->param('typeid');
        $nickname = $this->request->param('nickname');
        $desc = $this->request->param('desc');
        $key_word = $this->request->param('key_word');
        $uid = $this->uid;
        $token = $this->request->param('token');
        $option = array(
            'url'=>$url,
            'typeid'=>$typeid,
            'nickname'=>$nickname,
            'desc'=>$desc,
            'key_word'=>$key_word,
            'uid'=>$uid,
            'token'=>$token,
        );
        $this->addPersonUrlByType($option,'normal');
    }

    /**
     * @title 个人URL的增加(根据url_string,加快)
     * @description 个人URL的增加(根据url_string,加快)，同步进行的任务只是检查和插入了公共URL表。插入个人URL表任务，会发送队列消息给到后台。后台再依次执行任务。正确性不一定能保证，但速度快（并发能力强）。
     * @author Admin ldy
     * @url /api/perdata/perAddFast
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:u  type:string require:1 default:1 desc:核心数据
     * @param name:typeid  type:string require:1 default:1 desc:类型id
     * @param name:nickname  type:string require:1 default:1 desc:标题
     * @param name:desc  type:string require:1 default:1 desc:描述
     * @param name:key_word  type:string require:1 default:1 desc:关键词
     * @time 2018/9/25  17:30
     */
    public function perAddFast(){
        $url = $this->request->param('u');
        $typeid = $this->request->param('typeid');
        $nickname = $this->request->param('nickname');
        $desc = $this->request->param('desc');
        $key_word = $this->request->param('key_word');
        $uid = $this->uid;
        $token = $this->request->param('token');
        $option = array(
            'url'=>$url,
            'typeid'=>$typeid,
            'nickname'=>$nickname,
            'desc'=>$desc,
            'key_word'=>$key_word,
            'uid'=>$uid,
            'token'=>$token,
        );
        $this->addPersonUrlByType($option,'fast');
    }

    /**
     * @title 个人URL的增加(根据url_string和微信的昵称,加快)
     * @description
     * @author Admin ldy
     * @url /api/perdata/perAddByWxNickName
     * @param name:wx  type:string require:1 default:1 desc:微信的昵称
     * @param name:u  type:string require:1 default:1 desc:核心数据
     * @param name:nickname  type:string require:1 default:1 desc:标题
     * @param name:desc  type:string require:1 default:1 desc:描述
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @time 2018/10/31  11:22
     */
    public function perAddByWxNickName(){
        $url = $this->request->param('u');
        $nickname = $this->request->param('nickname');
        $desc = $this->request->param('desc');
        $wx = $this->request->param('wx');
        $this->checkEmptyParams($wx);
        $this->checkEmptyParams($url);
        $this->checkEmptyParams($nickname);
        $this->checkEmptyParams($desc);

        //UID通过用户的昵称获取
        $re = \app\common\library\YuAccount::getUidByWxNickName($wx);
        if (!$re->getStatus()){
            $this->err($re->getMsg());
        }
        $reData = $re->getData();
        $uid= $reData['id'];
        //获取用户的微信文件夹
        $getUserWxTypeIdRe1 = \app\common\library\YuUnDeleteType::getUserWxTypeIdByUserId($uid);
        if (!$getUserWxTypeIdRe1->getStatus()){
            $checkFirstTypeRe = \app\common\library\YuUnDeleteType::checkFirstTypeByUid($uid);
            if ($checkFirstTypeRe->getStatus()){
                $getUserWxTypeIdRe2 = \app\common\library\YuUnDeleteType::getUserWxTypeIdByUserId($uid);
                if ($getUserWxTypeIdRe2->getStatus()){
                    $re3 = $getUserWxTypeIdRe1->getData();
                    $typeid = $re3['id'];
                }else{
                    $this->err($getUserWxTypeIdRe2->getMsg(),'0');
                }
            }else{
                $this->err($checkFirstTypeRe->getMsg(),'1');
            }
        }else{
            $re4 = $getUserWxTypeIdRe1->getData();
            $typeid = $re4['id'];
        }

        $key_word = '微信收藏';
        //业务上应该不需要鉴权了，因为是通过用户昵称传递过来的，昵称只能用户自己绑定
        $token = '';
        $option = array(
            'url'=>$url,
            'typeid'=>$typeid,
            'nickname'=>$nickname,
            'desc'=>$desc,
            'key_word'=>$key_word,
            'uid'=>$uid,
            'token'=>$token,
        );
        $this->addPersonUrlByType($option,'fast');
    }


    protected function addPersonUrlByType($option,$type = 'fast'){
        $re = \app\common\library\YuPerdata::addPersonUrlByType($option,$type);
        if ($re->getStatus()){
            $this->success($re->getMsg(),$re->getData());
        }else{
            $this->err($re->getMsg());
        }
        /*$url = $option['url'];
        $typeid = $option['typeid'];
        $nickname = $option['nickname'];
        $desc = $option['desc'];
        $key_word = $option['key_word'];
        $uid = $option['uid'];
        $token = $option['token'];
        $data = [
            'typeid' => $typeid,
            'u'=>$url,
            'nickname'=>$nickname,
            'desc'=>$desc,
            'key_word'=>$key_word,
        ];
        $validate = new \app\common\validate\PerUrl;
        if (!$validate->scene('perAddByU')->check($data)) {
            $this->error($validate->getError());
        }
        //先操作增加公共URL数据
        $re = \app\common\library\YuDa::addPublicUrl($url,$token);
        if (!($re instanceof \app\common\library\YuResult)){
            //如果返回的不是这个格式的，报错
            $this->err(ErrorType::DATA_EXCEPTION);
        }
        if (!($re->getStatus())){
            //新增公共url操作失败
            $this->err(ErrorType::ADD_DATA_FAILED,$re->getData());
        }
        //在操作增加个人的url
        $data['token'] = $token;
        $data['uid'] = $uid;
        if ($type=='normal'){
            $re =  \app\common\library\YuDa::addPersonUrl($url,$token,$data);
            if ($re->getCode()){
                rewardNormal($uid,'新增【链接】成功！');
                $this->success('新增成功！',$re->getData(),200);
            }else{
                $this->err(ErrorType::ADD_DATA_FAILED,$re->getData());
            }
        }else if($type == 'fast'){
            $jobHandlerClassName = 'app\common\job\AddPersonUrl';
            $jobData = ['token' => $token, 'u' => $url,'data'=>json_encode($data)];
            $isPushed = \app\common\library\YuUtil::postMsg($jobHandlerClassName, $jobData);
            if ($isPushed) {
                $pushTime = date('Y-m-d H:i:s') . " a new AddPersonUrl Job is Pushed to the MQ" . "<br>";
                rewardNormal($uid,'新增【链接】成功！');
                $this->success('新增成功！',$pushTime,200);
            } else {
                $pushTime = 'Oops, something went wrong.';
                $this->err(ErrorType::ADD_DATA_FAILED,$pushTime);
            }
        }else{
            //这里可以扩展
            $this->err(ErrorType::PARAMS_CAN_NOT_BE_EMPTY);
        }*/

    }

    /**
     * @title 个人URL的增加(根据pid)(速度快，推荐)
     * @description 个人URL的增加(根据公共url类的public_url_id)
     * @author Admin ldy
     * @url /api/perdata/perAddByPId
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:public_url_id  type:string require:1 default:1 desc:公共url的id
     * @param name:typeid  type:string require:1 default:1 desc:类型id
     * @param name:nickname  type:string require:1 default:1 desc:标题
     * @param name:desc  type:string require:1 default:1 desc:描述
     * @param name:key_word  type:string require:1 default:1 desc:关键词
     * @time 2018/9/25  17:30
     */
    public function perAddByPId(){
        $public_url_id = $this->request->param('public_url_id');
        $typeid = $this->request->param('typeid');
        $nickname = $this->request->param('nickname');
        $desc = $this->request->param('desc');
        $key_word = $this->request->param('key_word');
        $token = $this->request->param('token');
        $uid = $this->uid;
        $data = [
            'typeid' => $typeid,
            'public_url_id'=>$public_url_id,
            'nickname'=>$nickname,
            'desc'=>$desc,
            'key_word'=>$key_word,
        ];
        $validate = new \app\common\validate\PerUrl;
        if (!$validate->scene('perAddByPId')->check($data)) {
            $this->error($validate->getError());
        }
        $da = array(
            'id'=>$public_url_id,
        );
        // 启动事务
        Db::startTrans();
        try {
            //检查是否已经存在该url
            $urlpublic = \app\common\model\UrlPublic::field('url,title_auto,id,socre,num_collecton,status_is_use')->where($da)->find();
            if (!$urlpublic) {
                //不存在的先插入到父亲后，在给个人添加？
                //还是自动插入？（无法自动插入，因为不存在数据）
                $this->err(ErrorType::DATA_EXCEPTION);
            }
            //存在父类的直接复制数据给个人
            //是否存在类型？（无法自动插入，因为不存在数据）
            $type = \app\common\model\Type::field('id')->where(array('id' => $typeid))->find();
            if (!$type) {
                $this->err(ErrorType::DATA_EXCEPTION);
            }
            //加载数据，让核心类处理业务
            $data['token'] = $token;
            $data['uid'] = $uid;
            $re =  \app\common\library\YuDa::addPersonUrl($urlpublic->url,$token,$data);

            // 提交事务
            Db::commit();
            if ($re->getCode()){
                rewardNormal($this->uid,'新增【链接】成功！');
                $this->success('新增成功！',$re->getData(),200);
            }else{
                // 回滚事务
                Db::rollback();
                $this->err(ErrorType::ADD_DATA_FAILED);
            }
        }catch (Exception $e){
            // 回滚事务
            Db::rollback();
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 获取用户的所有url
     * @description 获取用户的所有url
     * @author Admin ldy
     * @url /api/perdata/getPerByUserId
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/9/27  16:57
     */
    public function getPerByUserId(){
        $uid = $this->uid;
        $where = array(
            'status_is_detele'=>0,
            'user_id'=>$uid,
        );
        try{
            $arr = Db::table('scj_url_person')->where($where)->select();
            if ($arr){
                $this->success('数据获取成功！',$arr,200);
            }else{
	    //这里有问题，如果一开始数据为空的时候，报错了，后续所有操作无法进行！！
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 获取用户的某条URL记录
     * @description 获取用户的某条URL记录
     * @author Admin ldy
     * @url /api/perdata/getPerById
     * @param name:token  type:string require:1 default:1 desc:
     * @param name:id  type:string require:1 default:1 desc:
     * @time 2018/9/27  16:58
     */
    public function getPerById(){
        $id = $this->request->param('id');
        $this->checkEmptyParams($id);
        $uid = $this->uid;
        $where = array(
            'a.id'=>$id,
            'a.user_id'=>$uid,
        );
        try{
            $arr = Db::table('scj_url_person')
                ->field('w.url,a.id,a.desc,a.nickname,a.keyword,a.type_id')
                ->alias('a')
                ->join('scj_url_public w','a.base_url_id = w.id')
                ->where($where)
                ->select();
//            $arr = Db::table('scj_url_person')->where('id', $id)->select();
            if ($arr){
                $this->success('数据获取成功！',$arr,200);
            }else{
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 根据类型获取用户的URL记录
     * @description 根据类型获取用户的URL记录
     * @author Admin ldy
     * @url /api/perdata/getPerByTypeId
     * @param name:token  type:string require:1 default:1 desc:
     * @param name:typeid  type:string require:1 default:1 desc:
     * @time 2018/9/27  16:58
     */
    public function getPerByTypeId(){
        $uid = $this->uid;
        $typeid = $this->request->param('typeid');
        $where = array(
            'a.type_id'=>$typeid,
            'a.user_id'=>$uid,
        );
        try{
            $arr = Db::table('scj_url_person')
                ->field('w.url,a.id,a.desc,a.nickname,a.keyword,a.type_id')
                ->alias('a')
                ->join('scj_url_public w','a.base_url_id = w.id')
                ->where($where)
                ->select();
//            $arr = Db::table('scj_url_person')->where($where)->select();
            if ($arr){
                $this->success('数据获取成功！',$arr,200);
            }else{
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 连表查询url信息（速度较慢）
     * @description 连表查询url信息（速度较慢）
     * @author Admin ldy
     * @url /api/perdata/getPerAuto
     * @param name:id  type:string require:1 default:1 desc:
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/9/27  17:11
     */
    public function getPerAuto(){
        $id = $this->request->param('id');
        if (empty($id)){
            $where = array();
        }else{
            $where = array('a.id'=>$id,'a.status_is_detele'=>0);
        }
        try{
            $ee = Db::table('scj_url_person')
                ->field('w.url,a.id,a.desc,a.nickname,a.keyword,a.type_id')
                ->alias('a')
                ->join('scj_url_public w','a.base_url_id = w.id')
                ->where($where)
                ->select();
            if ($ee){
                //获取类型的父类数据
                if (!empty($id)) {
                    $typeid = $ee[0]['type_id'];
                    $uid = $this->uid;
                    $re =  \app\common\library\YuDa::getTypeStringById($typeid,$uid);
                    if ($re->getStatus()){
                        $ee[0]['type_string'] = $re->getData();
                    }
                }
                $this->success('数据获取成功！',$ee,200);
            }else{
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 删除某条URL
     * @description 删除某条URL
     * @author Admin ldy
     * @url /api/perdata/removePerUrl
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:id  type:string require:1 default:1 desc:用户url_id
     * @time 2018/9/28  9:03
     */
    public function removePerUrl(){
        $id = $this->request->param('id');
        $con = array(
          'id'=>$id,
        );
        try {
            //检查是否已经存在该url
            $urlperson = \app\common\model\UrlPerson::field('id,status_is_detele')->where($con)->find();
            if (!$urlperson) {
                $this->err(ErrorType::ACTION_FAILED);
            }
            $urlperson->status_is_detele = 1;
            $re = $urlperson->save();
            if ($re) {
                rewardNormal($this->uid,'删除【链接】成功！');
                $this->success('删除成功', $re,200);
            } else {
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 修改某条URL
     * @description 修改某条URL
     * @author Admin ldy
     * @url /api/perdata/updatePerUrl
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:id  type:string require:1 default:1 desc:用户url_id
     * @param name:typeid  type:string require:1 default:1 desc:类型id
     * @param name:desc  type:string require:1 default:1 desc:注解说明
     * @param name:nickname  type:string require:1 default:1 desc:名字
     * @param name:key_word  type:string require:1 default:1 desc:关键词
     * @param name:color  type:string require:0 default:0 desc:颜色
     * @time 2018/9/28  9:03
     */
    public function updatePerUrl(){
        $id = $this->request->param('id');
        $typeid = $this->request->param('typeid');
        $nickname = $this->request->param('nickname');
        $desc = $this->request->param('desc');
        $key_word = $this->request->param('key_word');
        $token = $this->request->param('token');
        $color = $this->request->param('color');
        $uid = $this->uid;
        $data = [
            'typeid' => $typeid,
            'id'=>$id,
            'nickname'=>$nickname,
            'desc'=>$desc,
            'key_word'=>$key_word,
            'color'=>$color
        ];
        $validate = new \app\common\validate\PerUrl;
        if (!$validate->scene('updatePerUrl')->check($data)) {
            $this->error($validate->getError());
        }
        $con = array(
            'id'=>$id,
        );
        try {
            //检查是否已经存在该url
            $urlperson = \app\common\model\UrlPerson::field('id,status_is_detele,num_read,num_collection,num_like,socre,version,type_id,color,desc,nickname,keyword')
                ->where($con)
                ->find();
            if (!$urlperson) {
                $this->err(ErrorType::ACTION_FAILED);
            }
            $urlperson->status_is_detele = 0;
            $urlperson->num_read = \app\common\library\YuUtil::addIntValue($urlperson->num_read);
            $urlperson->num_collection = \app\common\library\YuUtil::addIntValue($urlperson->num_collection);
            $urlperson->num_like = \app\common\library\YuUtil::addIntValue($urlperson->num_like);
            $urlperson->socre = \app\common\library\YuUtil::addIntValue($urlperson->socre);
            $urlperson->version = \app\common\library\YuUtil::addIntValue($urlperson->version);
            $urlperson->type_id = $typeid;
            $urlperson->color = $color;
            $urlperson->desc = $desc;
            $urlperson->nickname = $nickname;
            $urlperson->keyword = $key_word;
            $re = $urlperson->save();
            if ($re) {
                $this->success('修改成功', $re,200);
            } else {
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 修改某条URL的标题名
     * @description 修改某条URL的标题名
     * @author Admin ldy
     * @url /api/perdata/updatePerUrlTitle
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:id  type:string require:1 default:1 desc:用户url_id
     * @param name:nickname  type:string require:1 default:1 desc:名字
     * @time 2018/9/28  9:03
     */
    public function updatePerUrlTitle(){
        $id = $this->request->param('id');
        $nickname = $this->request->param('nickname');
        $data = [
            'id'=>$id,
            'nickname'=>$nickname,
        ];
        $validate = new \app\common\validate\PerUrl;
        if (!$validate->scene('updatePerUrlTitle')->check($data)) {
            $this->error($validate->getError());
        }
        $con = array(
            'id'=>$id,
        );
        try {
            //检查是否已经存在该url
            $urlperson = \app\common\model\UrlPerson::field('id,status_is_detele,num_read,num_collection,num_like,socre,version,type_id,color,desc,nickname,keyword')
                ->where($con)
                ->find();
            if (!$urlperson) {
                $this->err(ErrorType::ACTION_FAILED);
            }
            $urlperson->status_is_detele = 0;
            $urlperson->num_read = \app\common\library\YuUtil::addIntValue($urlperson->num_read);
            $urlperson->num_collection = \app\common\library\YuUtil::addIntValue($urlperson->num_collection);
            $urlperson->num_like = \app\common\library\YuUtil::addIntValue($urlperson->num_like);
            $urlperson->socre = \app\common\library\YuUtil::addIntValue($urlperson->socre);
            $urlperson->version = \app\common\library\YuUtil::addIntValue($urlperson->version);
            $urlperson->nickname = $nickname;
            $re = $urlperson->save();
            if ($re) {
                rewardNormal($this->uid,'修改【'.$nickname.'】成功！');
                $this->success('修改成功', $urlperson,200);
            } else {
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }

    }

    /**
     * @title 修改某条URL的批注
     * @description 修改某条URL的批注
     * @author Admin ldy
     * @url /api/perdata/updatePerUrlDesc
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:id  type:string require:1 default:1 desc:用户url_id
     * @param name:desc  type:string require:1 default:1 desc:注释
     * @time 2018/9/28  9:03
     */
    public function updatePerUrlDesc(){
        $id = $this->request->param('id');
        $desc = $this->request->param('desc');
        $this->checkEmptyParams($id);
        $con = array(
            'id'=>$id,
        );
        try {
            //检查是否已经存在该url
            $urlperson = \app\common\model\UrlPerson::field('id,status_is_detele,num_read,num_collection,num_like,socre,version,type_id,color,desc,nickname,keyword')
                ->where($con)
                ->find();
            if (!$urlperson) {
                $this->err(ErrorType::ACTION_FAILED);
            }
            $urlperson->status_is_detele = 0;
            $urlperson->num_read = \app\common\library\YuUtil::addIntValue($urlperson->num_read);
            $urlperson->num_collection = \app\common\library\YuUtil::addIntValue($urlperson->num_collection);
            $urlperson->num_like = \app\common\library\YuUtil::addIntValue($urlperson->num_like);
            $urlperson->socre = \app\common\library\YuUtil::addIntValue($urlperson->socre);
            $urlperson->version = \app\common\library\YuUtil::addIntValue($urlperson->version);
            $urlperson->desc = $desc;
            $re = $urlperson->save();
            if ($re) {
                rewardNormal($this->uid,'修改【批注】成功！');
                $this->success('修改成功', $urlperson,200);
            } else {
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 移动某条URL到一个目录
     * @description 移动某条URL到一个目录
     * @author Admin ldy
     * @url /api/perdata/movePerUrl
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:id  type:string require:1 default:1 desc:用户url_id
     * @param name:typeid  type:string require:1 default:1 desc:类型id
     * @time 2018/9/28  9:03
     */
    public function movePerUrl(){
        $id = $this->request->param('id');
        $typeid = $this->request->param('typeid');

        $data = [
            'typeid' => $typeid,
            'id'=>$id,
        ];
        $validate = new \app\common\validate\PerUrl;
        if (!$validate->scene('movePerUrl')->check($data)) {
            $this->error($validate->getError());
        }
        $con = array(
            'id'=>$id,
        );
        try {
            //检查是否已经存在该url
            $urlperson = \app\common\model\UrlPerson::field('id,status_is_detele,num_read,num_collection,num_like,socre,version,type_id,color,desc,nickname,keyword')
                ->where($con)
                ->find();
            if (!$urlperson) {
                $this->err(ErrorType::ACTION_FAILED);
            }
            $urlperson->status_is_detele = 0;
            $urlperson->num_read = \app\common\library\YuUtil::addIntValue($urlperson->num_read);
            $urlperson->num_collection = \app\common\library\YuUtil::addIntValue($urlperson->num_collection);
            $urlperson->num_like = \app\common\library\YuUtil::addIntValue($urlperson->num_like);
            $urlperson->socre = \app\common\library\YuUtil::addIntValue($urlperson->socre);
            $urlperson->version = \app\common\library\YuUtil::addIntValue($urlperson->version);
            $urlperson->type_id = $typeid;
            $re = $urlperson->save();
            if ($re) {
                rewardNormal($this->uid,'移动【链接】成功！');
                $this->success('修改成功', $re,200);
            } else {
                $this->err(ErrorType::DATA_EXCEPTION);
            }
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
    }

    /**
     * @title 简单展示分享的用户数据，
     * @description 分享的用户数据，简单展示版，待修改
     * @author Admin ldy
     * @url /api/perdata/simpleShow
     * @param name:id  type:string require:1 default:1 desc:
     * @time 2018/10/19  16:46
     */
    public function simpleShow(){
        $uid = $this->request->param('id');
        if (!empty($uid)){
//            $uid = 14;
            $where = array('a.user_id'=>$uid,'a.status_is_detele'=>0);
        }else{
            $where=array();
        }
        try {
            $array = Db::table('scj_url_person')
                ->field('w.url,a.id,a.desc,a.nickname,a.keyword')
                ->alias('a')
                ->join('scj_url_public w','a.base_url_id = w.id')
                ->where($where)
                ->select();
            if ($array){
//                var_dump(json_encode($array));
                foreach ($array as $item) {
//                    $str = '- '.$item['id'] .' '. $item['nickname'].'<br/> ';
//                    echo '- '.$item['id'] .' '. $item['nickname'].'<br/> ';
                    echo '- '.$item['id'].' <a href="'.$item['url'].'" target="_blank">'.$item['nickname'] . '</a><br />';
                }
            }
        }catch (Exception $e){
            $this->err(ErrorType::DATA_EXCEPTION);
        }
    }


    /**
     * @title 获取用户的url，分页加载
     * @description 获取用户的url，分页加载
     * @author Admin ldy
     * @url /api/perdata/getPerUrlByPage
     * @param name:token  type:string require:1 default:1 desc:
     * @param name:page  type:string require:1 default:1 desc:需要展示的页数
     * @time 2018/10/15  13:50
     */
    public function getPerUrlByPage(){
        $page = $this->request->param('page');
        $this->checkEmptyParams($page);
        //获取用户的消息，分页加载
        $re = \app\common\library\YuPerdata::getPerDataByUserId($this->uid,$page);
        if ($re->getStatus()){
            $this->success('成功！',$re->getData(),200);
        }else{
            $this->err($re->getMsg());
        }
    }





    public function test(){
        echo Date('Y-m-d');
        echo '<br>开始签到日期：'.Sign::$startDate;
        $token = '81cdc341-7dd5-46ef-acea-4f7e09c8445e';
        $uid = $this->getUidByToken($token);
        echo '<br>今天签到了吗？：'.Sign::isSignToday($uid);

//        echo '<br>给22号补签：'.Sign::recordSomeDate($uid,'2018-09-22');
        echo '<br>22号签到了吗？：'.Sign::getStatusByDate($uid,'2018-09-22');
        echo '<br>23号签到了吗？：'.Sign::getStatusByDate($uid,'2018-09-23');
        echo '<br>24号签到了吗？：'.Sign::getStatusByDate($uid,'2018-09-24');
        echo '<br>25号签到了吗？：'.Sign::getStatusByDate($uid,'2018-09-25');
        echo '<br>26号签到了吗？：'.Sign::getStatusByDate($uid,'2018-09-26');
        echo '<br>27号签到了吗？：'.Sign::getStatusByDate($uid,'2018-09-27');
        echo '<br>28号签到了吗？：'.Sign::getStatusByDate($uid,'2018-09-28');
//        $cache = \Think\Facade\Cache::handler();
//        $cache = Cache::init();
//        $handler = $cache->handler();

//        $da = \Think\Facade\Cache::tag('login-token')->get('');
//        var_dump($cache->tag('login-token')->get('*'));

        // 使用Redis缓存
//        Cache::store('redis')->set('name','value',3600);
//        $dd = Cache::store('redis')->get('name');
//        echo $dd;

    }






}
